package dicapi

import (
	"github.com/labstack/echo"
	// "time"
	"github.com/dgrijalva/jwt-go"
	// "net/http"
	// mw "github.com/labstack/echo/middleware"

	// . "github.com/hb-go/echo-web/conf"
	// "github.com/hb-go/echo-web/middleware/opentracing"
	// "oxy_server/module/cache"
	"oxy_server/middleware"
	"oxy_server/model"
	"oxy_server/module/log"
	brandpkg "oxy_server/res/dicserver/brand"
	"oxy_server/res/dicserver/category"
	"oxy_server/res/dicserver/dimensions"
	genderpkg "oxy_server/res/dicserver/gender"
	"oxy_server/res/dicserver/kind"
	"oxy_server/res/dicserver/material"
	picturepkg "oxy_server/res/dicserver/picture"
	"oxy_server/res/dicserver/property"
	"oxy_server/res/dicserver/providertype"
	rolepkg "oxy_server/res/dicserver/role"
	"oxy_server/res/dicserver/serialnumber"
	"oxy_server/res/dicserver/sizerecommend"
	"oxy_server/res/dicserver/typecode"
	"oxy_server/res/dicserver/versioncode"

	"gopkg.in/mgo.v2/bson"
)

const (
	SecretKey = "welcome to wangshubo's blog"
)

//-----
// API Routers
//-----
func Routers(e *echo.Echo) *echo.Echo {
	e.Logger.SetPrefix("api")
	// Routers
	// login
	e.POST("/api/login", UserLoginHandler)
	e.POST("/api/logout", UserLogoutHandler)
	// 性别
	g := genderpkg.New()
	pic := picturepkg.New()
	// 角色
	role := rolepkg.New()
	pro := property.New("old")
	prov3 := property.New("v3")
	// 品牌
	b := brandpkg.New()
	productkind := kind.New(2)
	newproductkind := kind.New(1)
	serial := serialnumber.New()
	typeCode := typecode.New()
	versionCode := versioncode.New()
	Dimensions := dimensions.New()
	categoryPac := category.New()
	materialPac := material.New()
	sizerecommendPac := sizerecommend.New()
	providert := providertype.New("v1")

	// r := e.Group("")
	api := e.Group("/api")
	api.Use(CheckSession)
	{
		// user
		api.GET("/user/list", GetUserListHandler)
		api.GET("/user", GetUserInfoHandler)
		api.POST("/user/add", CreateUserHandler)
		api.POST("/user/update", EditUserHandler)
		api.DELETE("/user", DelUserHandler)

		// gender
		api.GET("/gender/list", g.GetGenderListHandler)
		api.POST("/gender/add", g.AddGenderHandler)
		api.POST("/gender/update", g.UpdateGenderHandler)
		api.DELETE("/gender", g.DelGenderHandler)

		// productcategory
		api.GET("/productcategory/list", GetProductCategoryListHandler)
		api.POST("/productcategory/add", AddProductCategoryHandler)
		api.POST("/productcategory/update", UpdateProductCategoryHandler)
		api.DELETE("/productcategory", DelProductCategoryHandler)

		// productkind
		api.GET("/productkind/list", productkind.GetProductKindListHandler)
		api.POST("/productkind/add", productkind.AddProductKindHandler)
		api.POST("/productkind/update", productkind.UpdateProductKindHandler)
		api.DELETE("/productkind", productkind.DelProductKindHandler)

		// newproductkind
		api.GET("/newproductkind/list", newproductkind.GetProductKindListHandler)
		api.POST("/newproductkind/add", newproductkind.AddProductKindHandler)
		api.POST("/newproductkind/update", newproductkind.UpdateProductKindHandler)
		api.DELETE("/newproductkind", newproductkind.DelProductKindHandler)

		// season
		api.GET("/season/list", GetSeasonListHandler)
		api.POST("/season/add", AddSeasonHandler)
		api.POST("/season/update", UpdateSeasonHandler)
		api.DELETE("/season", DelSeasonHandler)

		// year
		api.GET("/year/list", GetYearListHandler)
		api.POST("/year/add", AddYearHandler)
		api.POST("/year/update", UpdateYearHandler)
		api.DELETE("/year", DelYearHandler)

		// skucolor
		api.GET("/skucolor/list", GetSkuColorListHandler)
		api.POST("/skucolor/add", AddSkuColorHandler)
		api.POST("/skucolor/update", UpdateSkuColorHandler)
		api.DELETE("/skucolor", DelSkuColorHandler)

		// skusize
		api.GET("/skusize/list", GetSkuSizeListHandler)
		api.POST("/skusize/add", AddSkuSizeHandler)
		api.POST("/skusize/update", UpdateSkuSizeHandler)
		api.DELETE("/skusize", DelSkuSizeHandler)

		// suppliertable
		api.GET("/suppliertable/list", GetSupplierTableListHandler)
		api.POST("/suppliertable/add", AddSupplierTableHandler)
		api.POST("/suppliertable/update", UpdateSupplierTableHandler)
		api.DELETE("/suppliertable", DelSupplierTableHandler)

		// roletable
		api.GET("/role/list", role.GetRoleListHandler)
		api.GET("/role", role.GetRoleInfoHandler)
		api.POST("/role/add", role.AddRoleHandler)
		api.POST("/role/update", role.UpdateRoleHandler)
		api.DELETE("/role", role.DelRoleHandler)

		// serialtable
		api.GET("/serial/list", serial.GetserialNumberListHandler)
		api.GET("/serial", serial.GetserialNumberInfoHandler)
		api.POST("/serial/add", serial.AddserialNumberHandler)
		api.POST("/serial/update", serial.UpdateserialNumberHandler)
		api.POST("/serial/updateall", serial.UpdateAllserialNumberHandler)
		api.DELETE("/serial", serial.DelserialNumberHandler)

		// suppliermodel
		api.GET("/suppliermodel/list", GetSupplierModelListHandler)
		api.POST("/suppliermodel/add", AddSupplierModelHandler)
		api.POST("/suppliermodel/update", UpdateSupplierModelHandler)
		api.DELETE("/suppliermodel", DelSupplierModelHandler)

		// propertytable
		api.GET("/property/list", pro.GetPropertyListHandler)
		api.GET("/property", pro.GetPropertyInfoHandler)
		api.POST("/property/add", pro.AddPropertyHandler)
		api.POST("/property/adds", pro.AddPropertysHandler)
		api.POST("/property/update", pro.UpdatePropertyHandler)
		api.DELETE("/property", pro.DelPropertyHandler)

		// propertytable v3
		api.GET("/v3/property/list", prov3.GetPropertyListHandler)
		api.GET("/v3/property", prov3.GetPropertyInfoHandler)
		api.POST("/v3/property/add", prov3.AddPropertyHandler)
		api.POST("/v3/property/adds", prov3.AddPropertysHandler)
		api.POST("/v3/property/update", prov3.UpdatePropertyHandler)
		api.DELETE("/v3/property", prov3.DelPropertyHandler)

		// picture
		api.GET("/picture/list", pic.GetPictureListHandler)
		api.POST("/picture/add", pic.AddPictureHandler)
		api.POST("/picture/update", pic.UpdatePictureHandler)
		api.DELETE("/picture", pic.DelPictureHandler)

		// brand
		api.GET("/brand/list", b.GetBrandListHandler)
		api.POST("/brand/add", b.AddBrandHandler)
		api.POST("/brand/update", b.UpdateBrandHandler)
		api.DELETE("/brand", b.DelBrandHandler)
		// typecode
		api.GET("/typecode/list", typeCode.GetListHandler)
		api.POST("/typecode/add", typeCode.AddHandler)
		api.POST("/typecode/update", typeCode.UpdateHandler)
		api.DELETE("/typecode", typeCode.DelHandler)
		// typecode
		api.GET("/versioncode/list", versionCode.GetVersionListHandler)
		api.POST("/versioncode/add", versionCode.AddVersionHandler)
		api.POST("/versioncode/update", versionCode.UpdateVersionHandler)
		api.DELETE("/versioncode", versionCode.DelVersionHandler)
		api.GET("/versioncode", versionCode.GetVersionInfoHandler)

		// deminsions 版师吗表
		api.GET("/dimensions/list", Dimensions.GetDimensionsListHandler)
		api.GET("/dimensions", Dimensions.GetDimensionsInfoHandler)
		api.POST("/dimensions/add", Dimensions.AddDimensionHandler)
		api.POST("/dimensions/adds", Dimensions.AddDimensionsHandler)
		api.POST("/dimensions/update", Dimensions.UpdateDimensionsHandler)
		api.DELETE("/dimensions", Dimensions.DelDimensionsHandler)

		// material 物料单码表 材质吗表
		api.GET("/material/list", materialPac.GetListHandler)
		api.POST("/material/add", materialPac.AddHandler)
		api.POST("/material/update", materialPac.UpdateHandler)
		api.DELETE("/material", materialPac.DelHandler)
		api.POST("/material/adds", materialPac.AddsHandler)

		// category 物料单码表 类别吗表
		api.GET("/category/list", categoryPac.GetListHandler)
		api.POST("/category/add", categoryPac.AddHandler)
		api.POST("/category/update", categoryPac.UpdateHandler)
		api.DELETE("/category", categoryPac.DelHandler)
		api.POST("/category/adds", categoryPac.AddsHandler)

		// 版师尺码推荐表
		api.GET("/sizerecommend/list", sizerecommendPac.GetListHandler)
		api.POST("/sizerecommend/add", sizerecommendPac.AddHandler)
		api.POST("/sizerecommend/update", sizerecommendPac.UpdateHandler)
		api.DELETE("/sizerecommend", sizerecommendPac.DelHandler)
		api.GET("/sizerecommend", sizerecommendPac.GetInfoHandler)
		// productkind
		api.GET("/providertype/list", providert.GetListHandler)
		api.POST("/providertype/add", providert.AddHandler)
		api.POST("/providertype/update", providert.UpdateHandler)
		api.DELETE("/providertype", providert.DelHandler)

	}
	return e
}
func CheckSession(next echo.HandlerFunc) echo.HandlerFunc {
	return func(c echo.Context) error {
		token, err := middleware.AuthToken(c)
		if err != nil || token == nil {
			log.Error("token检验失败")
			c.JSON(401, map[string]interface{}{
				"msg":  "error",
				"info": "token检验失败",
			})
			return nil
		}
		claims := token.Claims.(jwt.MapClaims)
		name := claims["name"].(string)
		role := claims["role"].(string)
		password := claims["password"].(string)
		model.DB().Dbname = "OxyCms"
		model.DB().Document = "users"
		_, count, err := model.DB().Query(bson.M{"username": name, "password": password, "role": role}, 1, 1)
		if err != nil && count > 0 {
			log.Error("数据库不存在用户认证失败")
			c.JSON(401, map[string]interface{}{
				"msg":  "error",
				"info": "用户认证失败",
			})
			return nil
		}
		return next(c)

	}
}

// type (
// 	HandlerFunc func(*Context) error
// )

// /**
//  * 自定义Context的Handler
//  */
// func handler(h HandlerFunc) echo.HandlerFunc {
// 	return func(c echo.Context) error {
// 		ctx := c.(*Context)
// 		return h(ctx)
// 	}
// }
